<?php

namespace App\Common\Services;

use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\FromCollection;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class CommonExcelExportWithTitle implements FromCollection, WithStyles
{

    //公共导出
    private $row;
    private $data;
    private $datawith;
    private $toptitle;

    public function __construct(array $row, array $data, array $datawith = [],string $toptitle='导出')
    {
        $this->row = $row;
        $this->data = $data;
        $this->datawith = $datawith;
        $this->toptitle = $toptitle;
    }

    /**
     * @return \Illuminate\Support\Collection
     */
    public function collection()
    {
        $row = $this->row;
        $data = $this->data;
        $toptitle=$this->toptitle;
        $row[1]=$row[0];
        //设置表头
        $keyinfo='';
        foreach ($row[1] as $key => $value) {

            if($keyinfo==''){
                $keyinfo=$key;
                $row[0][$key]=$toptitle;
            }else{
                $row[0][$key]='';
            }
            $key_arr[] = $key;
        }

        //输入数据
        foreach ($data as $key => &$value) {
            $js = [];
            for ($i = 0; $i < count($key_arr); $i++) {
                $js = array_merge($js, [$key_arr[$i] => $value[$key_arr[$i]] ?? '']);
            }
            array_push($row, $js);
            unset($value);
        }
        return collect($row);
    }

    public function styles(Worksheet $sheet)
    {

        if (count($this->datawith)) {
            foreach ($this->datawith as $key => $val) {
                $sheet->getColumnDimension($key)->setWidth($val);
            }
        }


        

        $titlecount=$this->titlecount();
        
        $sheet->mergeCells('A1:'.$titlecount.'1');
        $sheet->getStyle('A1:'.$titlecount.'1')->applyFromArray(['alignment' => ['horizontal' => 'left']]);
        $sheet->getStyle('A1:'.$titlecount.'1')->getFont()->setSize(20);
        $sheet->getRowDimension('1')->setRowHeight(40);
        $sheet->getStyle('A1:'.$titlecount.'1')->getAlignment()->setVertical('center');
        $sheet->getStyle('A1')->getFont()->setBold(true);


        $sheet->getStyle('A2:'.$titlecount.'2')->applyFromArray(['alignment' => ['horizontal' => 'center']]);
        $sheet->getStyle('A2:'.$titlecount.'2')->getFont()->setSize(14);

        // $sheet->getStyle('A1:C1')->getFont()->getColor()
        //     ->setRGB('FF0000');

        $sheet->getRowDimension('2')->setRowHeight(25);
        $sheet->getStyle('A2:'.$titlecount.'2')->getAlignment()->setVertical('center');
        $sheet->getStyle('A2:'.$titlecount.'2')->getFill()
        ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
        ->getStartColor()->setRGB('999999');


        
        $sheet->getStyle('A2:'.$titlecount.(count($this->data)+2))->applyFromArray(['alignment' => ['horizontal' => 'center']]);
        $sheet->getStyle('A2:'.$titlecount.(count($this->data)+2))->getAlignment()->setVertical('center');




    }

    protected function titlecount(){
        $number = count($this->row[0]);
        if(ceil($number/26)==1){
            $remainder = $number % 26; // 1-26
            $excelColumn = $remainder>0 ?chr($remainder + 64):'Z';
        }elseif(ceil($number/26)==2){
            $remainder = $number % 26; // 1-26
            $excelColumn = $remainder>0 ?'A'.chr($remainder + 64):'AZ';
        }elseif(ceil($number/26)==3){
            $remainder = $number % 26; // 1-26
            $excelColumn = $remainder>0 ?'B'.chr($remainder + 64):'BZ';
        }

        return $excelColumn;

    }
}
